package utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zcfeng.ischool.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import bean.Affair;
import bean.Articlebean;

public class TreeViewAdapter_2 extends BaseExpandableListAdapter {

	public static final int ItemHeight = 120;
	public static final int PaddingLeft = 50;
	private int myPaddingLeft = 0;

	static public class TreeNode {
		public Object parent;
		public List<HashMap<String,String>> childs = new ArrayList<>();
	}

	List<TreeNode> treeNodes = new ArrayList<TreeNode>();
	Context parentContext;

	public TreeViewAdapter_2(Context context, int myPaddingLeft) {
		parentContext = context;
		this.myPaddingLeft = myPaddingLeft;

	}

	public List<TreeNode> getTreeNode() {
		return treeNodes;
	}

	public void updateTreeNode(List<TreeNode> nodes) {
		treeNodes = nodes;
	}

	public void removeAll() {
		treeNodes.clear();
	}

	@Override
	public HashMap<String, String> getChild(int groupPosition, int childPosition) {
		if(childPosition<getChildrenCount(groupPosition))
		return treeNodes.get(groupPosition).childs.get(childPosition);
		else
			return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	static public TextView getTextView(Context context) {
		@SuppressWarnings("deprecation")
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, ItemHeight);
		TextView textView =new TextView(context);
		textView.setTextColor(Color.BLACK);
		textView.setLayoutParams(lp);
		textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
		return textView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
							 boolean isLastChild, View convertView, ViewGroup parent) {
		LinearLayout ll=null;
		if(childPosition+1==getChildrenCount(groupPosition))
		{
			ll=(LinearLayout) View.inflate(this.parentContext, R.layout.xlistview_footer, null);
			return ll;
		}
		ll=(LinearLayout) View.inflate(this.parentContext, R.layout.view_second_itemlist_style, null);
		TextView  tv1=(TextView) ll.findViewById(R.id.title);
		tv1.setTextColor(Color.BLACK);
		tv1.setText(getChild(groupPosition,childPosition).get("content")+"");
		TextView  tv2=(TextView) ll.findViewById(R.id.content);
		if(groupPosition==0)
			tv2.setText("描述："+getChild(groupPosition,childPosition).get("description")+"");
		if(groupPosition==1)
			tv2.setText("发起人："+getChild(groupPosition,childPosition).get("sponsor")+"");
		TextView  tv3=(TextView) ll.findViewById(R.id.time);
		tv3.setText("时间："+getChild(groupPosition,childPosition).get("date")+"");
		return ll;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return treeNodes.get(groupPosition).childs.size()+1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return treeNodes.get(groupPosition).parent;
	}

	@Override
	public int getGroupCount() {
		return treeNodes.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
							 View convertView, ViewGroup parent) {
		TextView textView = getTextView(this.parentContext);
		textView.setText(getGroup(groupPosition).toString());
		textView.setGravity(Gravity.CENTER_VERTICAL);
		textView.setPadding(myPaddingLeft + (PaddingLeft << 1), 0, 0, 0);
		return textView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
